/*
 * Created by Huang 嵘才 on 19-9-20 下午6:23
 * Copyright (c) 2019. All rights reserved.
 * Last modified 19-9-20 下午6:21
 */

package org.esir.base.android.thread

import org.esir.lib_java.thread.ThreadPool
import io.esirong.thread.PlatformThread
import java.lang.Exception

/**（-JAVA）
 * 线程管理功能模块类
 * Created by 黄嵘才(HuangRc) on 2019/9/20.
 */
object ThreadManager {

    private val platformThread: PlatformThread = AndroidMain()

    init {
    }

    /**
     * 在线程里执行一个代码段
     *
     * @param runnable {@linkplain Runnable}
     */
    @JvmStatic
    fun execute(runnable: Runnable) {
        ThreadPool.execute(runnable)
    }

    /**
     * 在Main线程里执行一个代码段
     *
     * @param runnable [Runnable]
     */
    @JvmStatic
    fun executeOnMain(runnable: Runnable) {
        platformThread.execute(runnable)
    }

    /**
     * 取消线程
     */
    @JvmStatic
    fun cancel(runnable: Runnable) {
        /*此处应该优化取消准确一点*/

        // 线程池取消
        try {
            ThreadPool.cancel(runnable)
        } catch (e: Exception) {
            e.printStackTrace()
        }

        //主线程取消
        try {
            platformThread.cancel(runnable)
        } catch (e: Exception) {
            e.printStackTrace()
        }

    }

}

